using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Infrastructure;
using Nop.Services.Logging;
using Nop.Services.Security;
using System;
using System.IO;
using System.Web.Mvc;
using System.Linq;
using Nop.Services.Authentication;
using System.Collections.Generic;
using Nop.Core.Domain;

namespace SpotsHub.Gateway.Controllers
{
    public class HomeController : Controller
    {
        #region Fields

        private readonly IPermissionService _permissionService;
        private readonly IWorkContext _workContext;
        private readonly StoreInformationSettings _storeInformationSettings;

        #endregion

        #region Ctor

        public HomeController(IPermissionService permissionService, IWorkContext workContext, StoreInformationSettings storeInformationSettings)
        {
            this._permissionService = permissionService;
            this._workContext = workContext;
            this._storeInformationSettings = storeInformationSettings;
        }

        #endregion

        #region Methods

        #region Pages

        // Addthis issue is not resolved, hence make the home page unsecured.
        [Nop.Web.Framework.Security.NopHttpsRequirement(Nop.Web.Framework.Security.SslRequirement.No)]
        public ActionResult Index()
        {
            var mobileDeviceHelper = EngineContext.Current.Resolve<Nop.Services.Common.IMobileDeviceHelper>();
            var mobileUrl = mobileDeviceHelper.RedirectUrlIfMobileDevice(this.HttpContext);

            if (!String.IsNullOrEmpty(mobileUrl))
            {
                return Redirect(mobileUrl);
            }

			return RedirectToRoute("HomePage");
        }

        public ActionResult SwitchHome(bool beta)
        {
            EnableBeta(beta);

            if (beta)
            {
                return RedirectToRoute("Preview");
            }

            return RedirectToAction("Index");
        }

        protected void EnableBeta(bool enableBeta)
        {
            if (enableBeta)
            {
                if (Request.Cookies[Nop.Web.Framework.HtmlExtensions.CookieKeyEnableBeta] == null)
                {
                    var c = new System.Web.HttpCookie(Nop.Web.Framework.HtmlExtensions.CookieKeyEnableBeta, "1");
                    c.Expires = DateTime.Now.AddDays(100);
                    Response.Cookies.Add(c);
                }
            }
            else if (Request.Cookies[Nop.Web.Framework.HtmlExtensions.CookieKeyEnableBeta] != null)
            {
                Response.Cookies[Nop.Web.Framework.HtmlExtensions.CookieKeyEnableBeta].Expires = DateTime.Now.AddDays(-1);
            }
        }

        protected bool BetaEnabled()
        {
            if (Request.Cookies[Nop.Web.Framework.HtmlExtensions.CookieKeyEnableBeta] != null)
            {
                if (_storeInformationSettings.StoreClosed)
                {
                    EnableBeta(false);
                    return false;
                }
                else
                {
                    return true;
                }
            }
            return false;
        }

        public ActionResult BerlinettaFerrariLandingPage()
        {
            return View();
        }
        public ActionResult AboutClubMembership()
        {
            return View();
        }

        public ActionResult ShippingRegulation()
        {
            return View();
        }

        public ActionResult OnlineShopping()
        {
            return View();
        }

        public ActionResult Testimonial()
        {
            return View();
        }

        public ActionResult Disclaimer()
        {
            return View();
        }

        public ActionResult Career()
        {
            return View();
        }

        public ActionResult Press()
        {
            return View();
        }

        public ActionResult OperatingHours()
        {
            ViewBag.Holidays = new List<Nop.Core.Domain.Directory.Holiday>();
            var holidaySerivce = EngineContext.Current.Resolve<Nop.Services.Directory.IHolidayService>();
            if (holidaySerivce != null)
            {
                ViewBag.Holidays = holidaySerivce.GetAllHolidays()
                    .ToList();
            }

            return View();
        }

        public ActionResult PrivacyPolicy()
        {
            return View();
        }

        public ActionResult FuelSurcharge()
        {
            return View();
        }

        public ActionResult QuestionsAndAnswers()
        {
            return View();
        }

        public ActionResult ContactSpotsHub()
        {
            return View();
        }

        public ActionResult AboutExpress()
        {
            return View();
        }

        public ActionResult SellOnline()
        {
            return View();
        }

        public ActionResult TermsAndConditions()
        {
            return View();
        }

        public ActionResult ExpeditedShippingExperienceEvent()
        {
            return View();
        }

        #endregion

        public ActionResult KeepAlive()
        {
            return Content("I am alive!");
        }

        #region Logistics

        public ActionResult OrderLogistics()
        {
            if (_workContext.CurrentCustomer.IsRegistered())
            {
                return RedirectToAction("PlaceOrder", "Order", new { area = "Logistics" });
            }

            return RedirectToAction("Login", "Customer", new { ReturnUrl = Url.RouteUrl("LogisticsOrder") });
        }

        #endregion

        #region Login

        public ActionResult Login(string returnUrl)
        {
            return RedirectToAction("Login", "Customer", new { area = "Logistics", ReturnUrl = returnUrl });
        }

        #endregion

        #region Logout

        public ActionResult Logout(string returnUrl)
        {
            return RedirectToAction("Logout", "Customer", new { area = "Logistics", ReturnUrl = returnUrl });
        }

        #endregion

        [HttpPost]
        public ActionResult GtResponse()
        {
            Stream httpBodyStream = this.Request.InputStream;
            string content = "none";
            if (httpBodyStream.Length > int.MaxValue)
            {
                //throw new ArgumentException("HTTP InputStream too large.");
            }
            else if (httpBodyStream.Length > 0)
            {

                TextReader tr = new StreamReader(httpBodyStream);
                content = tr.ReadToEnd();

                var logger = Nop.Core.Infrastructure.EngineContext.Current.Resolve<ILogger>();

                logger.Information(String.Format("Response is called: {0}", content));
            }
            return Content(content);
        }

        #endregion
    }
}
